package com.SmartClassroom.system.service.impl;

import com.SmartClassroom.system.common.ErrorCode;
import com.SmartClassroom.system.exception.BusinessException;
import com.SmartClassroom.system.mapper.AttendanceMapper;
import com.SmartClassroom.system.model.domain.Attendance;
import com.SmartClassroom.system.model.dto.attendance.AttendanceQueryRequest;
import com.SmartClassroom.system.model.vo.AttendanceVO;
import com.SmartClassroom.system.service.AttendanceService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author baijiayu
 * @description 针对表【Attendance】的数据库操作Service实现
 * @createDate 2024-11-19 11:01:51
 */
@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance> implements AttendanceService {

    @Resource
    private AttendanceMapper attendanceMapper;

    @Override
    public Page<AttendanceVO> getQueryWrapper(Page<AttendanceVO> pageParam, AttendanceQueryRequest attendanceQueryRequest) {
        if (attendanceQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }

        // 自定义分页查询
        List<AttendanceVO> attendanceVOList = attendanceMapper.page(pageParam, attendanceQueryRequest);
        if (attendanceVOList == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询失败");
        }

        for (AttendanceVO attendanceVO : attendanceVOList) {
            if("Monday".equals(attendanceVO.getDayOfWeek())) {
                attendanceVO.setDayOfWeek("周一");
            } else if("Tuesday".equals(attendanceVO.getDayOfWeek())) {
                attendanceVO.setDayOfWeek("星期二");
            } else if("Wednesday".equals(attendanceVO.getDayOfWeek())) {
                attendanceVO.setDayOfWeek("星期三");
            } else if("Thursday".equals(attendanceVO.getDayOfWeek())) {
                attendanceVO.setDayOfWeek("星期四");
            }
            if("Friday".equals(attendanceVO.getDayOfWeek())) {
                attendanceVO.setDayOfWeek("星期五");
            } else if("Saturday".equals(attendanceVO.getDayOfWeek())) {
                attendanceVO.setDayOfWeek("星期六");
            } else if("Sunday".equals(attendanceVO.getDayOfWeek())) {
                attendanceVO.setDayOfWeek("星期日");
            }
        }


        // 设置分页结果
        pageParam.setRecords(attendanceVOList);
        return pageParam;
    }

    @Override
    public Page<AttendanceVO> getQueryById(Page<AttendanceVO> pageParam, AttendanceQueryRequest attendanceQueryRequest,Long userId) {
        if (attendanceQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }

        // 自定义分页查询
        List<AttendanceVO> attendanceVOList = attendanceMapper.pageById(pageParam, attendanceQueryRequest, userId);
        if (attendanceVOList == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询失败");
        }

        // 设置分页结果
        pageParam.setRecords(attendanceVOList);
        return pageParam;
    }
}




